package com.cgnpc.scp.common.service.impl;

import com.cgnpc.cud.core.domain.AjaxResult;
import com.cgnpc.cuddemo.utils.IDHelper;
import com.cgnpc.scp.common.exception.ScpException;
import com.cgnpc.scp.common.mybatis.entity.ScpCommonFileUpload;
import com.cgnpc.scp.common.mybatis.mapper.client.ScpCommonFileUploadMapper;
import com.cgnpc.scp.common.pojo.UserInfo;
import com.cgnpc.scp.common.service.IScpCommonFileUploadService;
import com.cgnpc.scp.common.utils.AjaxResultUtil;
import com.cgnpc.scp.common.utils.JacksonUtil;
import com.cgnpc.scp.common.utils.ListUtil;
import com.cgnpc.scp.common.utils.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.UUID;

@Service
public class ScpCommonFileUploadServiceImpl implements IScpCommonFileUploadService {
    private static final Logger LOGGER = LoggerFactory.getLogger(ScpCommonFileUploadServiceImpl.class);

    @Autowired
    private ScpCommonFileUploadMapper scpCommonFileUploadMapper;

    @Autowired
    private MessageSource messageSource;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public <T extends ScpCommonFileUpload> AjaxResult save(UserInfo loginUserInfo, Locale reqLocale, List<T> reqEntityList) {
        final String logInfo = "[ScpCommonFileUploadServiceImpl_save_UUID_" + UUID.randomUUID().toString() + "]";

        try {
            if (ListUtil.getListSizeWith0(reqEntityList) <= 0) {
                return AjaxResultUtil.createI18nErrorDefaultMessage(reqLocale, messageSource);
            }

            Date nowDate = new Date();
            // begin 获取登录用户的信息
            String loginUserInfoUserId = null;
            String loginUserInfoUserName = null;

            if (loginUserInfo != null) {
                loginUserInfoUserId = loginUserInfo.getUserId();
                loginUserInfoUserName = loginUserInfo.getUserName();
            }
            // end 获取登录用户的信息

            LOGGER.info("{}printInfo;loginUserInfo:[{}];reqEntityList:[{}];"
                    , logInfo
                    , JacksonUtil.writeValueAsString(loginUserInfo)
                    , JacksonUtil.writeValueAsString(reqEntityList)
            );

            for (T reqEntity : reqEntityList) {
                if (StringUtil.isNullOrSpace(reqEntity.getId())) {
                    reqEntity.setId(IDHelper.uuidNew());
                }

                // begin 设置公共的字段
                reqEntity.setCreateTime(nowDate);
                reqEntity.setCreateUser(loginUserInfoUserId);
                reqEntity.setCreateUserName(loginUserInfoUserName);
                reqEntity.setLastUpdateTime(nowDate);
                reqEntity.setLastUpdateUser(loginUserInfoUserId);
                reqEntity.setLastUpdateUserName(loginUserInfoUserName);
                // end 设置公共的字段

                int saveRows = scpCommonFileUploadMapper.insert(reqEntity);

                if (saveRows <= 0) {
                    reqEntity.setMessage("save.fail");
                } else {
                    reqEntity.setMessage("save.success");
                }
            }

            return AjaxResultUtil.createI18nSuccessDefaultMessage(reqLocale, messageSource, reqEntityList);
        } catch (Exception e) {
            LOGGER.error("{}error;", logInfo, e);
            throw new ScpException(e);
        }
    }

    @Override
    public <T extends ScpCommonFileUpload> List<T> loadById(List<String> reqIdList) {
        if (ListUtil.getListSizeWith0(reqIdList) <= 0) {
            return null;
        }

        return (List<T>) scpCommonFileUploadMapper.selectBatchIds(reqIdList);
    }
}
